package com.corpgovernment.organization.operatelog.service;

import com.corpgovernment.api.costcenter.model.CostCenter;
import com.corpgovernment.api.costcenter.model.GetCostCenterListRequest;
import com.corpgovernment.api.operatelog.chooseconstant.ChooseBusinesstype;
import com.corpgovernment.api.organization.model.bookingrelation.AddOrUpdateBRReq;
import com.corpgovernment.api.organization.model.bookingrelation.ListBRReq;
import com.corpgovernment.api.organization.model.bookingrelation.UidOrgIdEntry;
import com.corpgovernment.api.organization.model.org.request.GetOrgInfoRequest;
import com.corpgovernment.api.organization.model.org.request.UpdateOrgRequest;
import com.corpgovernment.api.organization.model.post.front.PostEditRequest;
import com.corpgovernment.api.organization.model.post.front.PostEditResponse;
import com.corpgovernment.api.organization.model.switchinfo.GetPayConfigRequest;
import com.corpgovernment.api.organization.model.switchinfo.PayConfigRequest;
import com.corpgovernment.api.organization.model.switchinfo.SaveSwitchInfoRequest;
import com.corpgovernment.api.organization.model.user.employee.front.OperateEmployeeRequest;
import com.corpgovernment.api.organization.model.user.employee.front.SubmitSelectEmpRequest;
import com.corpgovernment.api.organization.model.user.employee.request.UpdateUserOrgRelationRequest;
import com.corpgovernment.api.supplier.vo.MbSupplierInfoVo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.operatelog.bo.OprerateLogBo;
import com.corpgovernment.common.operatelog.constant.OperateLogContant;
import com.corpgovernment.common.operatelog.enums.MethodEnum;
import com.corpgovernment.common.operatelog.enums.PojoTypeEnum;
import com.corpgovernment.costcenter.service.impl.CostCenterServiceImpl;
import com.corpgovernment.organization.constant.CommonConst;
import com.corpgovernment.organization.constant.OrganizationConst;
import com.corpgovernment.organization.entity.db.MbOrgEmployeeInfo;
import com.corpgovernment.organization.operatelog.service.pojo.*;
import com.corpgovernment.organization.service.BookingRelationService;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.corpgovernment.organization.service.impl.OrganizationEmployeeServiceImpl;
import com.corpgovernment.organization.service.impl.OrganizationInfoServiceImpl;
import com.corpgovernment.organization.service.impl.PostServiceImpl;
import com.corpgovernment.organization.service.impl.SwitchServiceImpl;
import com.corpgovernment.permission.controller.RoleRest;
import com.corpgovernment.permission.controller.UserRoleRest;
import com.corpgovernment.permission.entity.db.MbRole;
import com.corpgovernment.permission.service.RoleService;
import com.corpgovernment.permission.service.UserRoleService;
import com.corpgovernment.permission.vo.ListRoleReq;
import com.corpgovernment.supplier.service.MbSupplierInfoService;
import com.corpgovernment.supplier.service.MbSupplierProductService;
import com.corpgovernment.supplier.service.SupplierCompanyService;
import com.corpgovernment.supplier.vo.ProductUrlVo;
import com.corpgovernment.supplier.vo.suppliercompany.AddOrUpdateRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.DeleteRequestVo;
import com.corpgovernment.supplier.vo.suppliercompany.InitRequestVo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * @ClassName: OperateLogService
 * @description: TODO
 * @author: jiang.luo
 * @date: Created in 19:40 2020/1/3
 * @Version: 1.0
 **/
@Slf4j
@Service
public class OperateLogService {

    @Autowired
    private OrganizationEmployeeServiceImpl organizationEmployeeService;

    @Autowired
    private OrganizationBaseService organizationBaseService;

    @Autowired
    private SwitchServiceImpl switchService;
    @Autowired
    private PostServiceImpl postService;

    @Autowired
    private BookingRelationService bookingRelationService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CostCenterServiceImpl costCenterService;
    @Autowired
    private MbSupplierProductService supplierProductService;
    @Autowired
    private MbSupplierInfoService supplierInfoService;

    @Autowired
    private OrganizationInfoServiceImpl organizationInfoService;

    @Autowired
    private SupplierCompanyService supplierCompanyService;
    /**
     * 转化成操作日志需要的数据
     *
     * @param oprerateLogBo
     * @return
     */
    public Object convertParams(OprerateLogBo oprerateLogBo, Integer params, Object returnType) {
        log.info("-----------oprerateLogBo----------------" + oprerateLogBo.getParams());
        //人员信息
        if (MethodEnum.saveEmpBaseInfo.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<OperateEmployeeRequest> employeeRequestList = JsonUtils.parseArray(oprerateLogBo.getParams(), OperateEmployeeRequest.class);
            OperateEmployeeRequest operateEmployeeRequest = employeeRequestList.get(0);
            if (CollectionUtils.isNotEmpty(employeeRequestList)) {
                MbOrgEmployeeInfo mbOrgEmployeeInfo = convertEmployeeInfo(operateEmployeeRequest);
                StaffAddPojo staffAddPojo = JsonUtils.convert(mbOrgEmployeeInfo, StaffAddPojo.class);
                staffAddPojo.setSta(operateEmployeeRequest.getStatus());
                OperateLogPojo operateLogPojo = organizationEmployeeService.convertStaffLogPojo(staffAddPojo);
                return operateLogPojo;
            }
        } else if (MethodEnum.updateRelationStatus.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<UpdateUserOrgRelationRequest> relationList = JsonUtils.parseArray(oprerateLogBo.getParams(), UpdateUserOrgRelationRequest.class);
            if (CollectionUtils.isNotEmpty(relationList)) {
                UpdateUserOrgRelationRequest updateUserOrgRelationRequest = relationList.get(0);
            }

        }
        //支付配置
        else if (MethodEnum.saveConfigInfo.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<PayConfigRequest> payConfigRequests = JsonUtils.parseArray(oprerateLogBo.getParams(), PayConfigRequest.class);
            if (CollectionUtils.isNotEmpty(payConfigRequests)) {
                PayConfigRequest payConfigRequest = payConfigRequests.get(0);
                GetPayConfigRequest getPayConfigRequest = JsonUtils.convert(payConfigRequest, GetPayConfigRequest.class);
                PayPojo payPojo = switchService.convertPayConfig(getPayConfigRequest);
                return payPojo;
            }
        }
        //职级管理编辑，新增，删除0：新增 1：编辑 2：删除
        else if (MethodEnum.posEdit.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<PostEditRequest> postEditRequests = JsonUtils.parseArray(oprerateLogBo.getParams(), PostEditRequest.class);
            if (CollectionUtils.isNotEmpty(postEditRequests)) {
                PostEditRequest postEditRequest = postEditRequests.get(0);
                //后置通知，取新增的返回值
                if (OperateLogContant.AFTER.equals(params)) {
                    Integer status = postEditRequest.getStatus();
                    if (OperateLogContant.POST_ADD.equals(status)) {
                        try {
                            JSONResult<PostEditResponse> o = (JSONResult<PostEditResponse>) returnType;
                            postEditRequest.setId(o.getData().getAddPosInfo().getId());
                        } catch (Exception e) {
                            log.error("类型转化失败，原因:{}", e.getMessage(), e);
                        }
                    }
                }
                PostEditPojo postEditPojo = postService.convertPojo(postEditRequest);
                return postEditPojo;
            }
        }
        //职级移除，添加员工
        else if (MethodEnum.submitSelectEmp.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {

            List<SubmitSelectEmpRequest> submitSelectEmpRequests = JsonUtils.parseArray(oprerateLogBo.getParams(), SubmitSelectEmpRequest.class);
            if (CollectionUtils.isNotEmpty(submitSelectEmpRequests)) {
                SubmitSelectEmpRequest submitSelectEmpRequest = submitSelectEmpRequests.get(0);
                PostPojo postPojo = postService.convertPojo(submitSelectEmpRequest, params);
                return postPojo;
            }
        }

        //差旅属性
        else if (MethodEnum.saveOrgSwitch.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<SaveSwitchInfoRequest> saveSwitchInfoRequests = JsonUtils.parseArray(oprerateLogBo.getParams(), SaveSwitchInfoRequest.class);
            if (CollectionUtils.isNotEmpty(saveSwitchInfoRequests)) {
                SaveSwitchInfoRequest submitSelectEmpRequest = saveSwitchInfoRequests.get(0);
                SwitchInfoPojo switchInfoPojo = switchService.convertPojo(submitSelectEmpRequest);
                switchInfoPojo.setStatus(params);
                return switchInfoPojo;
            }
        }
        //代订权限
        else if (OperateLogContant.BOOKADD.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            //1新增2编辑3停用4启用5删除
            List<AddOrUpdateBRReq> add = JsonUtils.parseArray(oprerateLogBo.getParams(), AddOrUpdateBRReq.class);
            if (CollectionUtils.isNotEmpty(add)) {
                AddOrUpdateBRReq addOrUpdateBRReq = add.get(0);
                ListBRReq req = new ListBRReq();
                req.setOrgId(addOrUpdateBRReq.getOrgId());
                BookingRelationPojo bPojo = bookingRelationService.convert(req);
                bPojo.setType(OperateLogContant.BOOKINGRELATIONADD);
                return bPojo;
            }
        } else if (OperateLogContant.BOOKDISABLE.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            List<UidOrgIdEntry> disable = JsonUtils.parseArray(oprerateLogBo.getParams(), UidOrgIdEntry.class);
            if (CollectionUtils.isNotEmpty(disable)) {
                UidOrgIdEntry uidOrgIdEntry = disable.get(0);
                ListBRReq req = new ListBRReq();
                req.setOrgId(uidOrgIdEntry.getOrgId());
                BookingRelationPojo bPojo = bookingRelationService.convert(req);
                bPojo.setType(OperateLogContant.BOOKINGRELATIONDISABLE);
                return bPojo;
            }
        } else if (OperateLogContant.BOOKENABLE.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            List<UidOrgIdEntry> enable = JsonUtils.parseArray(oprerateLogBo.getParams(), UidOrgIdEntry.class);
            if (CollectionUtils.isNotEmpty(enable)) {
                UidOrgIdEntry uidOrgIdEntry = enable.get(0);
                ListBRReq req = new ListBRReq();
                req.setOrgId(uidOrgIdEntry.getOrgId());
                BookingRelationPojo bPojo = bookingRelationService.convert(req);
                bPojo.setType(OperateLogContant.BOOKINGRELATIONENABLE);
                return bPojo;
            }
        } else if (OperateLogContant.BOOKDELETE.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            List<UidOrgIdEntry> delete = JsonUtils.parseArray(oprerateLogBo.getParams(), UidOrgIdEntry.class);
            if (CollectionUtils.isNotEmpty(delete)) {
                UidOrgIdEntry uidOrgIdEntry = delete.get(0);
                ListBRReq req = new ListBRReq();
                req.setOrgId(uidOrgIdEntry.getOrgId());
                BookingRelationPojo bPojo = bookingRelationService.convert(req);
                bPojo.setType(OperateLogContant.BOOKINGRELATIONDEL);
                return bPojo;
            }
        }
        //角色管理
        else if (MethodEnum.addOrUpdate.toString().equalsIgnoreCase(oprerateLogBo.getMethodName())) {
            List<RoleRest.RoleInfo> roleInfoList = JsonUtils.parseArray(oprerateLogBo.getParams(), RoleRest.RoleInfo.class);
            if (CollectionUtils.isNotEmpty(roleInfoList)) {
                RoleRest.RoleInfo roleInfo = roleInfoList.get(0);
                ListRoleReq req = new ListRoleReq();
                req.setCorpId(roleInfo.getOrgId());
                RoleInfoPojo roleInfoPojo = userRoleService.convertPojo(req);
                if (Objects.isNull(roleInfoPojo)) {
                    return null;
                }
                if(roleInfo.getId() == null){
                    roleInfoPojo.setType(OperateLogContant.ROLEADD);
                }else {
                    roleInfoPojo.setType(OperateLogContant.ROLEUPDATE);
                }
                return roleInfoPojo;
            }
        }else if (OperateLogContant.ROLE_DELETE_SIGN.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            List<RoleRest.RoleId> roleInfoList = JsonUtils.parseArray(oprerateLogBo.getParams(), RoleRest.RoleId.class);
            if (CollectionUtils.isNotEmpty(roleInfoList)) {
                RoleRest.RoleId roleInfo = roleInfoList.get(0);
                MbRole load = roleService.load(roleInfo.getId());
                if (Objects.isNull(load)) {
                    log.info("roleService#load查询为null id为：{}", roleInfo.getId());
                    return null;
                }
                RoleInfoPojo roleInfoPojo = new RoleInfoPojo();
                roleInfoPojo.setDesc(load.getDescription());
                roleInfoPojo.setName(load.getName());
                roleInfoPojo.setType(OperateLogContant.ROLEDEL);
                return roleInfoPojo;
            }
        }
        else if (OperateLogContant.USERROLEADDOREDIT.equalsIgnoreCase(oprerateLogBo.getRemark())) {
            List<UserRoleRest.InsertOrUpdate> insertOrUpdates = JsonUtils.parseArray(oprerateLogBo.getParams(), UserRoleRest.InsertOrUpdate.class);
            if (CollectionUtils.isNotEmpty(insertOrUpdates)) {
                UserRoleRest.InsertOrUpdate insertOrUpdate = insertOrUpdates.get(0);
                UserRoleRest.UserRolePageReq req = new UserRoleRest.UserRolePageReq();
                req.setCorpId(insertOrUpdate.getOrgId());
                UserRolePojo userRolePojo = userRoleService.convert(req);
                if (Objects.isNull(userRolePojo)) {
                    log.info("userRoleService#convert为null,{}", JsonUtils.toJsonString(req));
                    return null;
                }
                if(insertOrUpdate.getId() != null){
                    userRolePojo.setStatus(OperateLogContant.ROLEUPDATE);
                }else {
                    userRolePojo.setStatus(OperateLogContant.ROLEADD);
                }
                return userRolePojo;
            }
        }else if(OperateLogContant.USERROLEDEL.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<UserRoleRest.InsertOrUpdate> insertOrUpdates = JsonUtils.parseArray(oprerateLogBo.getParams(), UserRoleRest.InsertOrUpdate.class);
            if (CollectionUtils.isNotEmpty(insertOrUpdates)) {
                UserRoleRest.InsertOrUpdate insertOrUpdate = insertOrUpdates.get(0);
                if(insertOrUpdate != null && insertOrUpdate.getId() != null){
                    MbRole mbRole = userRoleService.loadRole(insertOrUpdate.getId());
                    UserRoleRest.UserRolePageReq req = new UserRoleRest.UserRolePageReq();
                    req.setCorpId(mbRole.getOrgId());
                    UserRolePojo userRolePojo = userRoleService.convert(req);
                    userRolePojo.setStatus(OperateLogContant.ROLEDEL);
                    return userRolePojo;
                }
            }
        }
        //成本中心
        else if(OperateLogContant.COSTINSERT.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<CostCenter> costCenters = JsonUtils.parseArray(oprerateLogBo.getParams(), CostCenter.class);
            if (CollectionUtils.isNotEmpty(costCenters)) {
                CostCenter costCenter = costCenters.get(0);
                GetCostCenterListRequest getCostCenterListRequest = new GetCostCenterListRequest();
                getCostCenterListRequest.setOrgId(costCenter.getCompanyId());
                CostCenterPojo cost = costCenterService.convert(getCostCenterListRequest);
                cost.setStatus(OperateLogContant.COSTADD);
                return cost;
            }
        }
        else if(OperateLogContant.COSTUPDATE.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<CostCenter> costCenters = JsonUtils.parseArray(oprerateLogBo.getParams(), CostCenter.class);
            if (CollectionUtils.isNotEmpty(costCenters)) {
                CostCenter costCenter = costCenters.get(0);
                GetCostCenterListRequest getCostCenterListRequest = new GetCostCenterListRequest();
                getCostCenterListRequest.setOrgId(costCenter.getCompanyId());
                CostCenterPojo cost = costCenterService.convert(getCostCenterListRequest);
                cost.setStatus(OperateLogContant.COSTUDPATE);
                return cost;
            }
        }
        else if(OperateLogContant.COSTDELETE.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<CostCenter> costCenters = JsonUtils.parseArray(oprerateLogBo.getParams(), CostCenter.class);
            if (CollectionUtils.isNotEmpty(costCenters)) {
                CostCenter costCenter = costCenters.get(0);
                GetCostCenterListRequest getCostCenterListRequest = new GetCostCenterListRequest();
                getCostCenterListRequest.setOrgId(costCenter.getCompanyId());
                CostCenterPojo cost = costCenterService.convert(getCostCenterListRequest);
                cost.setStatus(OperateLogContant.COSTDEL);
                return cost;
            }
        }
        //URL接口授权维护
        else if(OperateLogContant.SUPPLIERINSERTOREDIT.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<ProductUrlVo> productUrlVoList = JsonUtils.parseArray(oprerateLogBo.getParams(), ProductUrlVo.class);
            if (CollectionUtils.isNotEmpty(productUrlVoList)) {
                ProductUrlVo productUrlVo = productUrlVoList.get(0);
                ProductUrlPojo urlPojo = supplierProductService.convertPojo();
                if(productUrlVo.getId() == null){
                    urlPojo.setStatus(OperateLogContant.SUPPLIER_ADD);
                }else {
                    urlPojo.setStatus(OperateLogContant.SUPPLIER_EDEIT);
                }
                return urlPojo;
            }
        }

        else if(OperateLogContant.SUPPLIERDEL.equalsIgnoreCase(oprerateLogBo.getRemark())){
            ProductUrlPojo urlPojo = supplierProductService.convertPojo();
            urlPojo.setStatus(OperateLogContant.SUPPLIER_DEL);
            return urlPojo;
        }
        //服务商管理
        else if(OperateLogContant.INSERTORUPDATESUPPLIERINFO.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<MbSupplierInfoVo> supplierInfoVoList = JsonUtils.parseArray(oprerateLogBo.getParams(), MbSupplierInfoVo.class);
            if (CollectionUtils.isNotEmpty(supplierInfoVoList)) {
                MbSupplierInfoVo supplierInfoVo = supplierInfoVoList.get(0);
                SupplierInfoPojo urlPojo = supplierInfoService.convertPojo();
                if(supplierInfoVo.getId() == null){
                    urlPojo.setStatus(OperateLogContant.SPLINFO_ADD);
                }else {
                    urlPojo.setStatus(OperateLogContant.SPLINFO_EDEIT);
                }
                return urlPojo;
            }
        }
        else if(OperateLogContant.DELETESUPPLIERINFO.equalsIgnoreCase(oprerateLogBo.getRemark())){
            SupplierInfoPojo urlPojo = supplierInfoService.convertPojo();
            urlPojo.setStatus(OperateLogContant.SPLINFO_DEL);
            return urlPojo;
        }
        //组织信息
        else if(OperateLogContant.UPDATEORGINFO.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<UpdateOrgRequest> updateOrgRequestList = JsonUtils.parseArray(oprerateLogBo.getParams(), UpdateOrgRequest.class);
            if (CollectionUtils.isNotEmpty(updateOrgRequestList)) {
                UpdateOrgRequest updateOrgRequest = updateOrgRequestList.get(0);
                GetOrgInfoRequest request = new GetOrgInfoRequest();
                request.setOrgId(updateOrgRequest.getContactInfo().getOrgId());
                OrgInfoPojo orgInfoPojo = organizationInfoService.convertOrgInfo(request);
                return orgInfoPojo;
            }
        }

        //企业订购业务
        else if(OperateLogContant.COMPANYADD.equalsIgnoreCase(oprerateLogBo.getRemark())
                || OperateLogContant.COMPANYUPDATE.equalsIgnoreCase(oprerateLogBo.getRemark()) ){
            List<AddOrUpdateRequestVo> addOrUpdateRequestVoList = JsonUtils.parseArray(oprerateLogBo.getParams(), AddOrUpdateRequestVo.class);
            if (CollectionUtils.isNotEmpty(addOrUpdateRequestVoList)) {
                AddOrUpdateRequestVo addOrUpdateRequestVo = addOrUpdateRequestVoList.get(0);
                InitRequestVo initRequestVo = new InitRequestVo();
                initRequestVo.setCompanyCode(addOrUpdateRequestVo.getSupplierCompany().getCompanyCode());
                initRequestVo.setProductType(addOrUpdateRequestVo.getSupplierCompany().getProductType());
                initRequestVo.setSupplierCode(addOrUpdateRequestVo.getSupplierCompany().getSupplierCode());
                CompanyPojo companyPojo = supplierCompanyService.convertCompanyPojo(initRequestVo);
                if(OperateLogContant.COMPANYADD.equalsIgnoreCase(oprerateLogBo.getRemark())){
                    companyPojo.setStatus(OperateLogContant.COMPANY_ADD);
                }else {
                    companyPojo.setStatus(OperateLogContant.COMPANY_UPDATE);
                }
                return companyPojo;
            }
        }
        else if(OperateLogContant.COMPANYDEL.equalsIgnoreCase(oprerateLogBo.getRemark())){
            List<DeleteRequestVo> deleteRequestVoList = JsonUtils.parseArray(oprerateLogBo.getParams(), DeleteRequestVo.class);
            if (CollectionUtils.isNotEmpty(deleteRequestVoList)) {
                DeleteRequestVo deleteRequestVo = deleteRequestVoList.get(0);
                CompanyPojo companyPojo = supplierCompanyService.convertDelPojo(deleteRequestVo);
                companyPojo.setStatus(OperateLogContant.COMPANY_DEL);
                return companyPojo;
            }
        }
        return null;
    }

    /**
     * 保存日志信息
     *
     * @param obj
     * @param object
     */
    public void saveLog(Object obj, Object object) {
        try {
            if (object != null) {
                String name = object.getClass().getSimpleName();
                if (PojoTypeEnum.OperateLogPojo.toString().equalsIgnoreCase(name)) {
                    OperateLogPojo pojo = (OperateLogPojo) object;
	                if (OperateLogContant.STAFF_ADD.intValue() == pojo.getSta()) {
		                //新增人员信息日志
		                organizationBaseService.insertLog(pojo, ChooseBusinesstype.ORGSTRUCTURELIST);
	                } else if (OperateLogContant.STAFF_UPDATE.intValue() == pojo.getSta()) {
		                OperateLogPojo old = (OperateLogPojo) obj;
		                //修改人员信息日志
		                organizationBaseService.updateLog(old, pojo, ChooseBusinesstype.ORGSTRUCTURELIST);
	                } else {

	                }
                } else if (PojoTypeEnum.PayPojo.toString().equalsIgnoreCase(name)) {
                    PayPojo pojo = (PayPojo) object;
                    PayPojo old = (PayPojo) obj;
                    organizationBaseService.updateLog(old, pojo, ChooseBusinesstype.PAYCONFIG);
                } else if (PojoTypeEnum.PostEditPojo.toString().equalsIgnoreCase(name)) {

                    PostEditPojo old = (PostEditPojo) obj;
                    PostEditPojo news = (PostEditPojo) object;
                    if (OperateLogContant.STAFF_ADD.equals(old.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.CONFIGPOSITION);
                    } else if (OperateLogContant.STAFF_UPDATE.equals(old.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.CONFIGPOSITION);
                    } else {
                        organizationBaseService.deleteLog(old, new PostEditPojo(), ChooseBusinesstype.CONFIGPOSITION);
                    }
                } else if (PojoTypeEnum.PostPojo.toString().equalsIgnoreCase(name)) {
                    PostPojo news = (PostPojo) object;
                    PostPojo old = (PostPojo) obj;
                    if(old != null){
                        List<PostInfoPojo> lodList = old.getList();
                        if (CollectionUtils.isNotEmpty(lodList)) {
                            if (OrganizationConst.DEPARTMENT.equalsIgnoreCase(old.getType())) {
                                lodList.forEach(o -> {
                                    organizationBaseService.insertLog(o, ChooseBusinesstype.CONFIGPOSITION);
                                });
                            } else {
                                lodList.forEach(o -> {
                                    if (o.getIsNew()) {
                                        organizationBaseService.insertLog(o, ChooseBusinesstype.CONFIGPOSITION);
                                    } else {
                                        organizationBaseService.deleteLog(o, new PostInfoPojo(), ChooseBusinesstype.CONFIGPOSITION);
                                    }
                                });
                            }
                        }
                    }
                    //如果是部门，只有新增，否则有删除跟新增
                    List<PostInfoPojo> list = news.getList();
                    if (CollectionUtils.isNotEmpty(list)) {
                        if (OrganizationConst.DEPARTMENT.equalsIgnoreCase(news.getType())) {
                            list.forEach(w -> {
                                organizationBaseService.insertLog(w, ChooseBusinesstype.CONFIGPOSITION);
                            });
                        } else {
                            list.forEach(w -> {
                                if (w.getIsNew()) {
                                    organizationBaseService.insertLog(w, ChooseBusinesstype.CONFIGPOSITION);
                                } else {
                                    organizationBaseService.deleteLog(w, new PostInfoPojo(), ChooseBusinesstype.CONFIGPOSITION);
                                }
                            });
                        }
                    }

                } else if (PojoTypeEnum.SwitchInfoPojo.toString().equalsIgnoreCase(name)) {

                    SwitchInfoPojo news = (SwitchInfoPojo) object;
                    SwitchInfoPojo old = (SwitchInfoPojo) obj;
                    if (OperateLogContant.AFTER.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.TRAVELCONFIGATTRIBUTES);
                    } else {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.TRAVELCONFIGATTRIBUTES);
                    }
                } else if (PojoTypeEnum.BookingRelationPojo.toString().equalsIgnoreCase(name)) {
                    BookingRelationPojo news = (BookingRelationPojo) object;
                    BookingRelationPojo old = (BookingRelationPojo) obj;
                    if (OperateLogContant.BOOKINGRELATIONADD.equals(news.getType())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.TRAVELCONFIGAGENTBOOKING);
                    } else if (OperateLogContant.BOOKINGRELATIONUPDATE.equals(news.getType())
                            || OperateLogContant.BOOKINGRELATIONDISABLE.equals(news.getType())
                            || OperateLogContant.BOOKINGRELATIONENABLE.equals(news.getType())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.TRAVELCONFIGAGENTBOOKING);
                    } else {
                        organizationBaseService.deleteLog(old, new BookingRelationPojo(), ChooseBusinesstype.TRAVELCONFIGAGENTBOOKING);
                    }
                }
                else if (PojoTypeEnum.RoleInfoPojo.toString().equalsIgnoreCase(name)) {
                    RoleInfoPojo old = (RoleInfoPojo) obj;
                    RoleInfoPojo news = (RoleInfoPojo) object;
                    if (OperateLogContant.ROLEADD.equals(news.getType())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.COMPANYSETTINGROLE);
                    } else if(OperateLogContant.ROLEUPDATE.equals(news.getType())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.COMPANYSETTINGROLE);
                    }else {
                        organizationBaseService.deleteLog(old, new RoleInfoPojo(), ChooseBusinesstype.COMPANYSETTINGROLE);
                    }
                }else if (PojoTypeEnum.UserRolePojo.toString().equalsIgnoreCase(name)){
                    UserRolePojo old = (UserRolePojo) obj;
                    UserRolePojo news = (UserRolePojo) object;
                    if (OperateLogContant.ROLEADD.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.COMPANYSETTINGROLE);
                    } else if(OperateLogContant.ROLEUPDATE.equals(news.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.COMPANYSETTINGROLE);
                    }else {
                        organizationBaseService.deleteLog(old, new UserRolePojo(), ChooseBusinesstype.COMPANYSETTINGROLE);
                    }
                }else if (PojoTypeEnum.CostCenterPojo.toString().equalsIgnoreCase(name)){
                    CostCenterPojo old = (CostCenterPojo) obj;
                    CostCenterPojo news = (CostCenterPojo) object;
                    if (OperateLogContant.COSTADD.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.COSTCENTER);
                    } else if(OperateLogContant.COSTUDPATE.equals(news.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.COSTCENTER);
                    }else {
                        organizationBaseService.deleteLog(old, new CostCenterPojo(), ChooseBusinesstype.COSTCENTER);
                    }
                }
                //URL接口授权维护
                else if (PojoTypeEnum.ProductUrlPojo.toString().equalsIgnoreCase(name)){
                    ProductUrlPojo old = (ProductUrlPojo) obj;
                    ProductUrlPojo news = (ProductUrlPojo) object;
                    if (OperateLogContant.SUPPLIER_ADD.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.SERVICEMANAGERAUTHORIZE);
                    } else if(OperateLogContant.SUPPLIER_EDEIT.equals(news.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.SERVICEMANAGERAUTHORIZE);
                    }else {
                        organizationBaseService.deleteLog(old, new ProductUrlPojo(), ChooseBusinesstype.SERVICEMANAGERAUTHORIZE);
                    }
                }

                else if (PojoTypeEnum.SupplierInfoPojo.toString().equalsIgnoreCase(name)){
                    SupplierInfoPojo old = (SupplierInfoPojo) obj;
                    SupplierInfoPojo news = (SupplierInfoPojo) object;
                    if (OperateLogContant.SPLINFO_ADD.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.SERVICEMANAGERSERVICELIST);
                    } else if(OperateLogContant.SPLINFO_EDEIT.equals(news.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.SERVICEMANAGERSERVICELIST);
                    }else {
                        organizationBaseService.deleteLog(old, new ProductUrlPojo(), ChooseBusinesstype.SERVICEMANAGERSERVICELIST);
                    }
                }
                else if (PojoTypeEnum.OrgInfoPojo.toString().equalsIgnoreCase(name)){
                    OrgInfoPojo old = (OrgInfoPojo) obj;
                    OrgInfoPojo news = (OrgInfoPojo) object;
                    organizationBaseService.updateLog(old, news, ChooseBusinesstype.ORGSTRUCTUREPOSITION);
                }

                else if (PojoTypeEnum.CompanyPojo.toString().equalsIgnoreCase(name)){
                    CompanyPojo old = (CompanyPojo) obj;
                    CompanyPojo news = (CompanyPojo) object;
                    if (OperateLogContant.COMPANY_ADD.equals(news.getStatus())) {
                        organizationBaseService.insertLog(news, ChooseBusinesstype.COMPANYBOOKING);
                    } else if(OperateLogContant.COMPANY_UPDATE.equals(news.getStatus())) {
                        organizationBaseService.updateLog(old, news, ChooseBusinesstype.COMPANYBOOKING);
                    }else {
                        organizationBaseService.deleteLog(old, new CompanyPojo(), ChooseBusinesstype.COMPANYBOOKING);
                    }
                }

            }else {
                if(obj != null){
                    String name = obj.getClass().getSimpleName();
                    if (PojoTypeEnum.RoleInfoPojo.toString().equalsIgnoreCase(name)) {
                        RoleInfoPojo old = (RoleInfoPojo) obj;
                        if (OperateLogContant.ROLEDEL.equals(old.getType())) {
                            organizationBaseService.deleteLog(old, new RoleInfoPojo(), ChooseBusinesstype.COMPANYSETTINGROLE);
                        }
                    }
                    else if (PojoTypeEnum.UserRolePojo.toString().equalsIgnoreCase(name)) {
                        UserRolePojo old = (UserRolePojo) obj;
                        if (OperateLogContant.ROLEDEL.equals(old.getStatus())) {
                            organizationBaseService.deleteLog(old, new UserRolePojo(), ChooseBusinesstype.COMPANYSETTINGROLE);
                        }
                    }
                    else if (PojoTypeEnum.CompanyPojo.toString().equalsIgnoreCase(name)) {
                        CompanyPojo old = (CompanyPojo) obj;
                        if (OperateLogContant.COMPANY_DEL.equals(old.getStatus())) {
                            organizationBaseService.deleteLog(old, new CompanyPojo(), ChooseBusinesstype.COMPANYBOOKING);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("调用保存方法失败！原因:{}", e.getMessage(), e);
        }
    }

    private MbOrgEmployeeInfo convertEmployeeInfo(OperateEmployeeRequest request) {
        String uid = request.getUid();
        MbOrgEmployeeInfo mbOrgEmployeeInfo = new MbOrgEmployeeInfo();
        mbOrgEmployeeInfo.setName(request.getUserName());
        mbOrgEmployeeInfo.setUid(uid);
        mbOrgEmployeeInfo.setLastEnName(request.getLastName());
        mbOrgEmployeeInfo.setFirstEnName(request.getFirstName());
        mbOrgEmployeeInfo.setMobilePhone(request.getTelPhone());
        mbOrgEmployeeInfo.setAreaCode(request.getAreaCode());
        mbOrgEmployeeInfo.setEmail(request.getMailbox());
        mbOrgEmployeeInfo.setOrgId(request.getOrgId());
        String state = request.getState();
        if (OperateLogContant.MAN_STATUS.equals(state)) {
            mbOrgEmployeeInfo.setValid(CommonConst.TRUE);
        } else {
            mbOrgEmployeeInfo.setValid(CommonConst.FALSE);
        }
        return mbOrgEmployeeInfo;
    }
}
